<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [广告位]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Adsense as M;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class Adsense extends Base
{
    //广告位列表
    public function index(){
        if(Request::isAjax()) {
            $list = M::order('sort','asc')->select();
            return ['code'=>0,'msg'=>'获取成功！','data'=>$list];
        }else{
            return view();
        }
    }
    //添加
    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            $result = M::create($data);
            if ($result) {
                return ['code'=>0,'msg'=>'添加成功'];
            } else {
                return ['code'=>1,'msg'=>'添加失败'];
            }
        }else{
            return view('form',['info' => '']);
        }
    }
    //编辑
    public function edit(){
        if(request()->isPost()) {
            $data=input('post.');
            $result = M::editSave($data);
            return $result;
        }else{
            $id = input('id');
            $info = M::find($id);
            return view('form',['info' =>$info]);
        }
    }
    //字段值修改
    public function cf(){
        $data = input('post.');
        $result = M::editSave($data);
        return $result;
    }
    // 删除
    public function del(){
        $id = Request::param('id');
        // 启动事务
        Db::startTrans();
        try {
            M::destroy($id);
            Db::name('ad')->where('as_id',$id)->delete();//删除该分组下广告
            // 提交事务
            Db::commit();
            return ['code'=>0,'msg'=>'删除成功！'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>1,'msg'=>'删除失败！'];
        }
    }

}